package com.cpi.cornpp.blockdistribution.converter;

import com.cpi.cornpp.blockdistribution.dto.BlockDistributionDTO;
import com.cpi.cornpp.blockdistribution.exception.BlockDistributionException;
import com.cpi.cornpp.blockdistribution.view.BlockDistributionView;
import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.purchaseorder.view.PurchaseOrderVersionView;
import com.cs.common.utils.NumberUtil;

public class BlockDistributionConverter {
	public BlockDistributionView convertDTOToView(PurchaseOrderVersionView purchaseOrderVersionView, BlockDistributionDTO blockDistributionDTO)throws BlockDistributionException{
		BlockDistributionView blockDistributionView = null;
		int periods = -1;
		double quantity = -1;
		Double capacity = null;
		try {
			capacity = purchaseOrderVersionView.getCapacity();
			
			blockDistributionView = new BlockDistributionView();
			blockDistributionView.setPurchaseOrderCode(purchaseOrderVersionView.getCode());
			blockDistributionView.setPurchaseOrderCapacity(NumberUtil.convertTon(capacity));
			if( blockDistributionDTO != null ){
				periods = blockDistributionDTO.getPeriodsNumber();
				quantity = blockDistributionDTO.getQuantity();
				
				blockDistributionView.setBlockedDistributionId(blockDistributionDTO.getBlockedDistributionId());
				blockDistributionView.setStartBlockPeriodId(blockDistributionDTO.getStartBlockPeriodId());
				blockDistributionView.setPeriodsNumber(NumberUtil.convertInteger(periods));
				blockDistributionView.setQuantity(NumberUtil.convertTon(quantity));
				blockDistributionView.setPurchaseOrderVersionId(blockDistributionDTO.getPurchaseOrderVersionId());
				blockDistributionView.setStartBlockPeriodDescription(blockDistributionDTO.getStartBlockPeriodDescription());
				blockDistributionView.setFinishBlockPeriodDescription(blockDistributionDTO.getFinishBlockPeriodDescription());
			}
		}catch (Exception sqlException){
			BlockDistributionException blockDistributionException = null;
			sqlException.printStackTrace(System.out);
			blockDistributionException = new BlockDistributionException (sqlException , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_SELECT);
			throw blockDistributionException;
		}
		return blockDistributionView;
	}
	
	public BlockDistributionDTO convertViewToDTO(BlockDistributionView blockDistributionView)throws BlockDistributionException{
		BlockDistributionDTO blockDistributionDTO = null;
		String periods = null;
		String quantity = null;
		try {
			blockDistributionDTO = new BlockDistributionDTO();
			
			periods = blockDistributionView.getPeriodsNumber();
			quantity = blockDistributionView.getQuantity();
			
			blockDistributionDTO.setBlockedDistributionId(blockDistributionView.getBlockedDistributionId());
			blockDistributionDTO.setPurchaseOrderVersionId(blockDistributionView.getPurchaseOrderVersionId());
			blockDistributionDTO.setStartBlockPeriodId(blockDistributionView.getStartBlockPeriodId());
			blockDistributionDTO.setPeriodsNumber(NumberUtil.parseInt(periods));
			blockDistributionDTO.setQuantity(NumberUtil.parseDouble(quantity));
		}catch (Exception sqlException){
			BlockDistributionException blockDistributionException = null;
			sqlException.printStackTrace(System.out);
			blockDistributionException = new BlockDistributionException (sqlException , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_UPDATE);
			throw blockDistributionException;
		}
		return blockDistributionDTO;
	}
}